package cn.harry.wx.service.impl;

import cn.harry.common.utils.SecurityUtils;
import cn.harry.common.utils.SysUserUtils;
import cn.harry.sys.entity.SysUser;
import cn.harry.wx.dao.CmsArticleDao;
import cn.harry.wx.entity.CmsArticle;
import cn.harry.wx.service.CmsArticleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * CMS文章中心
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Service("cmsArticleService")
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleDao, CmsArticle> implements CmsArticleService {


    @Override
    public IPage<CmsArticle> getPage(CmsArticle cmsArticle, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<CmsArticle> wrapper = new LambdaQueryWrapper<>(cmsArticle);
        SysUser user = SysUserUtils.getSysUser();
        if (!SecurityUtils.isAdmin(user.getId())) {
            wrapper.eq(CmsArticle::getUserId, user.getId());
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }


    @Override
    public CmsArticle selectById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int create(CmsArticle cmsArticle) {
        return this.baseMapper.insert(cmsArticle);
    }


    @Override
    public int update(Long id, CmsArticle cmsArticle) {
        cmsArticle.setId(id);
        return this.baseMapper.updateById(cmsArticle);
    }


    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }


}